package com.example.basic.web.controller.user;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.core.result.Result;
import com.example.basic.core.result.ResultGenerator;
import com.example.basic.custom.Log;
import com.example.basic.web.model.user.User;
import com.example.basic.web.service.user.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;


/**
 * <p>
 * 人员管理 前端控制器
 * </p>
 */
@Api(value = "人员管理相关操作", tags = {"人员管理"})
@RestController
@RequestMapping("/api/v1/byg/user")
public class UserController {
    @Resource
    private UserService userService;

    @Log(title = "人员管理", description = "新增修改")
    @ApiOperation(value="新增修改",notes = "新增修改")
    @PostMapping("/addAndUpdate")
    public Result<User> addAndUpdate(@ApiParam(name = "user", value = "模型", required = true) @RequestBody User user) {
        return ResultGenerator.genSuccessResult(userService.addAndUpdate(user));
    }

    @Log(title = "人员管理", description = "停用启用")
    @ApiOperation(value="停用启用",notes = "停用启用")
    @PostMapping("/enabled")
    public Result<Boolean> enabled(@ApiParam(value = "用户编码",name = "userCode") @RequestParam String userCode) {
        return ResultGenerator.genSuccessResult(userService.enabled(userCode));
    }

    @Log(title = "人员管理", description = "锁定解锁")
    @ApiOperation(value="锁定解锁",notes = "锁定解锁")
    @PostMapping("/lockUnlock")
    public Result<Boolean> lockUnlock(@ApiParam(value = "用户编码",name = "userCode") @RequestParam String userCode) {
        return ResultGenerator.genSuccessResult(userService.lockUnlock(userCode));
    }

    @Log(title = "人员管理", description = "删除")
    @ApiOperation(value="删除",notes = "删除")
    @PostMapping("/remove")
    public Result<Boolean> remove(@ApiParam(value = "用户编码",name = "userCode") @RequestParam String userCode) {
        return ResultGenerator.genSuccessResult(userService.remove(userCode));
    }

    @ApiOperation(value="重置用户密码",notes = "重置用户密码")
    @PostMapping("/resetPwd")
    public Result<Boolean> resetPwd(
            @ApiParam(name = "loginName", value = "需要重置的登录名", required = true)@RequestParam String loginName,
            @ApiParam(name = "loginUserCode",value = "操作人唯一代码") @RequestParam String loginUserCode) {
        return ResultGenerator.genSuccessResult(userService.resetPwd(loginName,loginUserCode));
    }

    @Log(title = "人员管理", description = "列表")
    @ApiOperation(value="列表",notes = "列表")
    @PostMapping("/queryList")
    public Result<IPage<User>> queryList(
            @ApiParam(value = "用户名/登录名",name = "searchParam") @RequestParam(required = false) String searchParam,
            @ApiParam(value = "状态，0-全部，1-启用，2-停用，3-锁定",name = "status",required = false) @RequestParam(defaultValue = "0") String status,
            @ApiParam(name = "page", value = "页数，list分页的第几页") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "条数，list分页的每页条数") @RequestParam(defaultValue = "0") Integer size) {
        Page<User> queryPage = new Page<>(page, size);
        return ResultGenerator.genSuccessResult(userService.queryList(queryPage,searchParam,status));

    }

    /**
     * 修改密码
     * @param loginName
     * @param oriPwd 加密后的原密码
     * @param newPwd 加密后的新密码
     * @return
     */
    @Log(title = "用户模块",description = "修改密码")
    @ApiOperation(value="修改密码",notes = "修改密码")
    @PostMapping("/modifyPwd")
    @ResponseBody
    public Result<Boolean> modifyPwd(@ApiParam(name = "loginName", value = "登录名", required = true) @RequestParam(required = true) String loginName,
                                    @ApiParam(name = "oriPwd", value = "原密码", required = true) @RequestParam(required = true) String oriPwd,
                                    @ApiParam(name = "newPwd", value = "新密码", required = true) @RequestParam(required = true) String newPwd) {

        return ResultGenerator.genSuccessResult(userService.modifyPwd(loginName,oriPwd,newPwd));
    }

    @Log(title = "人员管理", description = "详情")
    @ApiOperation(value="详情",notes = "详情")
    @PostMapping("/detail")
    public Result<User> detail(@ApiParam(value = "用户编码",name = "userCode") @RequestParam String userCode) {
        return ResultGenerator.genSuccessResult(userService.detail(userCode));
    }
}